package com.LeeCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 数对和
 */

public class Code面试题16_24 {
    public static void main(String[] args) {
        int[] nums = {5, 6, 5, 6};
        int target = 11;
        System.out.println(new Code面试题16_24().pairSums(nums, target));
    }

    public List<List<Integer>> pairSums(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        List<List<Integer>> ans = new ArrayList<>();
        for (int num : nums) {
            int toMap = target - num;
            if (map.containsKey(toMap)) {
                ans.add(List.of(toMap, num));
                map.merge(toMap, -1, (oldVal, delta) -> {
                    int res = oldVal + delta;
                    return res == 0 ? null : res;
                });
            } else {
                map.merge(num, 1, Integer::sum);
            }
        }
        return ans;
    }
}
